#include "htf_ai_backend_factory.h"
#include "mindspore/htf_ms_backend.h"
#include "native/htf_native_backend.h"
#include <stdlib.h>

htf_ai_backend_t* htf_ai_backend_create(int backend_type) {
    htf_ai_backend_t* backend = (htf_ai_backend_t*)calloc(1, sizeof(htf_ai_backend_t));
    if (!backend) return NULL;

    switch (backend_type) {
        case HTF_BACKEND_MINDSPORE:
            backend->impl = htf_ms_backend_create();
            backend->ops = htf_ms_backend_get_ops();
            break;
        case HTF_BACKEND_NATIVE:
            backend->impl = htf_native_backend_create();
            backend->ops = htf_native_backend_get_ops();
            break;
        default:
            free(backend);
            return NULL;
    }

    if (!backend->impl) {
        free(backend);
        return NULL;
    }

    return backend;
}

void htf_ai_backend_destroy(htf_ai_backend_t* backend) {
    if (backend) {
        if (backend->ops && backend->ops->destroy) {
            backend->ops->destroy(backend->impl);
        }
        free(backend);
    }
}